package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.qf.entity.Emp;
import com.qf.service.EmpService;
import com.qf.util.R;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping("/emp/{empno}")
    public R findEmpByEmpno(@PathVariable int empno){
        Emp user=empService.findEmpByEmpno(empno);
        if(user==null){
            return R.fail(5000,"查无此人");
        }
        return R.success(user);
    }
    @GetMapping("/emp/list")
    public R findAll(Emp emp){
        List<Emp> e=empService.findAll();
        if(emp==null){
            return R.fail(5000,"没有数据");
        }
        return R.success(e);
    }
    @GetMapping("/emp/add")
    public R addEmp(Emp emp){
        int a=empService.addEmp(emp);
        if(a<0){
            return R.fail(5000,"添加失败");
        }
        return R.success("添加成功");
    }
    @GetMapping("/emp/delete/{empno}")
    public  R deleteByEmpno(@PathVariable int empno){
        int d=empService.deleteByEmpno(empno);
        if(d<0){
            return R.fail(5000,"删除失败");
        }
        return R.success("删除成功");
    }
    @GetMapping("/emp/update")
    public R updateEmp(Emp emp){
        int u=empService.updateEmp(emp);
        if(u<0){
            return R.fail(5000,"更新失败");
        }
        return R.success("更新成功");
    }
    //动态sqlwhere+if
    @GetMapping("/emp/search")
    public R searchEmp(@RequestParam HashMap<String,Object> map){
        List<Emp> list=empService.searchEmp(map);
        return R.success(list);
    }
    //动态sql set
    @GetMapping("/emp/update2")
    public R updateEmp2(Emp emp){
        empService.updateEmp2(emp);
        return R.success();
    }
    @GetMapping("/emp/delete")
    public R deleteEmp(@RequestParam List< Integer> nos){
        empService.deleteEmp(nos);
        return R.success();
    }
    //分页
    @GetMapping("/emp/page")
    public R empPage(int pageSize,int pageNum){
        PageHelper.startPage(pageSize,pageNum);
        List<Emp> list=empService.findAll();
        return R.success(list);
    }
}
